#include "CBaseView.h"
#include "StringOps.h"
#include"Sonar.h"

using namespace std;

CBaseView::CBaseView():m_pxOut(&std::cout)
{
}

CBaseView::~CBaseView()
{
}

void CBaseView::generateHeader(){
std::string sRelativizer = "uboat/";
	*m_pxOut <<"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"<<endl;
	*m_pxOut <<"<html>"<<endl;
	*m_pxOut <<"<head>"<<endl;
	*m_pxOut <<"<title>" << m_pxRadar->m_xCodebaseRoot.filename()<< " - Neptuner UBoat" "</title>";
	*m_pxOut <<"<link rel='shortcut icon' type='image/ico' href='"<<sRelativizer<<"neptunerIcon.jpg'>"<<endl;
	*m_pxOut <<"		<link rel='stylesheet' type='text/css' media='all' href='uboat/style.css' />"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"prototype.js'></script>"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"fastinit.js'></script>"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"tablesort.js'></script>"<<endl;
	*m_pxOut <<"	</head>"<<endl;
}

void CBaseView::generateBody()
{
	*m_pxOut <<"	<body>"<<endl;

	*m_pxOut <<"<img src='uboat/neptunerIcon.jpg'><strong>Neptuner UBoat</strong></img><br/><br/>"<<endl;
	*m_pxOut <<"<b>Codebase Name: </b>"<<m_pxRadar->m_xCodebaseRoot.filename()<<"<br/>"<<endl;
	*m_pxOut <<"<b>Codebase Root: </b>"<< m_pxRadar->m_xCodebaseRoot<<"<br/>"<<endl;
	*m_pxOut <<"<b>Include Root : </b>"<< m_pxRadar->m_xIncludeRoot<<"<br/>"<<endl;
	*m_pxOut <<"<b>Files Parsed : </b>"<<m_pxScubaPack->size()<<"<br/>"<<endl;
	*m_pxOut <<"<HR/>"<<endl;
	*m_pxOut <<"<ul><b>Usage Notes</b>"<<endl;
	*m_pxOut <<"<li>Follow the links to browse the codebase files.</li>"<<endl;
	*m_pxOut <<"<li>Left-click on icons to bring up menus.</li>"<<endl;
	*m_pxOut <<"<li>Double-click on lines when they change color, to add content to them or edit them.</li>"<<endl;
	*m_pxOut <<"<li>Click on titles of the columns in tables to sort them.</li>"<<endl;
	*m_pxOut <<"</ul>"<<endl;
	*m_pxOut <<"<HR/>"<<endl;
						   
	*m_pxOut<<"			<table class='sortable'>" <<endl;

	m_asOutputRow.push_back("Filename");
	m_asOutputRow.push_back("Lines");
	m_asOutputRow.push_back("Statements");
	m_asOutputRow.push_back("LoC");
	m_asOutputRow.push_back("LoCom");
	m_asOutputRow.push_back("Tokens");
	m_asOutputRow.push_back("Tabs");
	m_asOutputRow.push_back("Spaces");
	m_asOutputRow.push_back("Characters");

	printHeading(false);
	printFooting();

	size_t i=0, n=m_pxScubaPack->size();
	Scuba* pxScuba=NULL;
	for(i=0;i<n;i++)
	{
		pxScuba = m_pxScubaPack->at(i);
		Sonar  xSonar;
		pxScuba->walk(xSonar);

		std::string sFilename = m_pxRadar->getCodebaseLocation(xSonar.m_pxLocation->m_sFilename);
		std::string sTarget = sFilename + ".uboat.html";
		std::string sAnchor = "<a href='codebase/" + sTarget + "'>" ;
		sAnchor += sFilename;
		sAnchor += "</a>";
		m_asOutputRow.push_back(sAnchor);

		m_asOutputRow.push_back(stringify(xSonar.m_nTotalLines));
		m_asOutputRow.push_back(stringify(xSonar.m_nStatements));
		m_asOutputRow.push_back(stringify(xSonar.m_nLoC));
		m_asOutputRow.push_back(stringify(xSonar.m_nLoCom));
		m_asOutputRow.push_back(stringify(xSonar.m_asToken.size()));
		m_asOutputRow.push_back(stringify(xSonar.m_nTabs));
		m_asOutputRow.push_back(stringify(xSonar.m_nSpaces));
		m_asOutputRow.push_back(stringify(xSonar.m_nCharacters));

		printElement();
	}


}

void CBaseView::generateFooter()
{
	*m_pxOut<<"			</table>"<<endl;
	*m_pxOut<<"	</body>"<<endl;
	*m_pxOut<<"</html>"<<endl;
}

void CBaseView::printElement(bool bClear)
{
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<td>"; sEnd = "</td>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;

	if(bClear) m_asOutputRow.clear();

}

void CBaseView::printFooting(bool bClear)
{
	*m_pxOut<<"			<tfoot>"<<endl;
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<td>"; sEnd = "</td>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;
	*m_pxOut<<"			</tfoot>"<<endl;

	if(bClear) m_asOutputRow.clear();
}

void CBaseView::printHeading(bool bClear)
{

	*m_pxOut<<"			<thead>" <<endl;
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<th>"; sEnd = "</th>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;
	*m_pxOut<<"			</thead>"<<endl;

	if(bClear) m_asOutputRow.clear();
}

void CBaseView::printBodyStart()
{
	*m_pxOut << "			<tbody>" << endl;
}

void CBaseView::printBodyEnd()
{
	*m_pxOut << "			</tbody>" << endl;
}




